<?php

/**
 * 特批发票控制器
 * @author yuzy 2015-5-13
 */

namespace mybackend\controllers;

use Yii;
use common\models\myhtsq\SpecialApprovalInvoiceModel;
use common\services\myhtsq\SpecialApprovalInvoiceService;
use common\entities\myhtsq\ActionCode;

class SpecialApprovalInvoiceController extends BaseController {

    protected $_invoiceService;

    public function __construct($id, $module, SpecialApprovalInvoiceService $invoiceService, $config = []) {
        $this->_invoiceService = $invoiceService;
        parent::__construct($id, $module, $config);
    }

    protected function roleAuthRules(){
        return [
            'actions'=>['GetSpecialApprovalInvoiceList','GetSpecialApprovalInvoiceInfo','SaveSpecialApprovalInvoice', 'DelSpecialApprovalInvoice','ExportSpecialApprovalInvoiceList'],
            'rules'=>[
                        [
                        'allow'=>true,
                        'actionCodeMaps'=>[
                            'GetSpecialApprovalInvoiceList'=>ActionCode::SPECIAL_APPROVE_INVOICE_VIEW,
                            'GetSpecialApprovalInvoiceInfo'=>ActionCode::SPECIAL_APPROVE_INVOICE_VIEW,
                            'SaveSpecialApprovalInvoice'=>ActionCode::SPECIAL_APPROVE_INVOICE__MNG,
                            'DelSpecialApprovalInvoice'=>ActionCode::SPECIAL_APPROVE_INVOICE__MNG,
                            'ExportSpecialApprovalInvoiceList' => ActionCode::TPFP_EXPORT,
                            ]
                        ]
                    ]
                ];
    }
    
    /**
     * 特批发票列表
     * @param integer $page 页码
     * @param integer $pageSize 页条数
     * @param string $sortBy 排序字段
     * @param boolean $sortAsc 是否升序
     * @param integer $areaId 区域id
     * @param string $keyword 关键字
     * @return array
     */
    public function actionGetSpecialApprovalInvoiceList($pageIndex, $pageSize, $sortBy, $sortAsc, $keyword, $areaId,$fulfil) {
        $result = $this->_invoiceService->getSpecialApprovalInvoiceList($pageIndex, $pageSize, $sortBy, $sortAsc, $keyword, $areaId,$fulfil);
        $this->exportJson($result, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }

    /**
     * 获取特批发票信息
     * @param string $invoiceId
     * @return \common\models\myhtsq\SpecialApprovalInvoiceModel
     */
    public function actionGetSpecialApprovalInvoiceInfo($invoiceId){
        $result = $this->_invoiceService->getSpecialApprovalInvoiceInfo($invoiceId);
        $this->exportJson($result, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg()); 
    }    
    
    /**
     * 保存特批发票
     */
    public function actionSaveSpecialApprovalInvoice() {
        $this->exitIfNotPost();
        $model = new SpecialApprovalInvoiceModel();
        if ($model->load($_POST['item'],'')) {
            $attachmentList = [];
            if(isset($_POST['attachmentList'])){
                $attachmentList = $_POST['attachmentList'];
            }
            if ($model->special_approval_invoice_id == '') {
                $result = $this->_invoiceService->addSpecialApprovalInvoice($model,$attachmentList);
            } else {
                $result = $this->_invoiceService->updateSpecialApprovalInvoice($model,$attachmentList);
            }
            
            $this->exportJson($result, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
        } else {
            $this->errorParam();
        }
    }
    
    /**
     * 导出特批发票
     * @param type $keyword 关键字
     * @param type $areaId 区域
     * @param type $fulfil 是否兑现
     */
    public function actionExportSpecialApprovalInvoiceList () {
        $keyword = $this->getParam('keyword','post');
        $areaId = $this->getParam('areaId','post');
        $fulfil = $this->getParam('fulfil','post');
        $data = $this->_invoiceService->getSpecialApprovalInvoiceList("", "", "created_on", "", $keyword, $areaId, $fulfil);
        $this->_invoiceService->exportSpecialApprovalInvoiceList($data['items']);
        $this->exportJson($data, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }

    /**
     * 删除特批发票
     */
    public function actionDelSpecialApprovalInvoice () {
        $invoiceId = $this->getParam('invoiceId','get');
        $result = $this->_invoiceService->delSpecialApprovalInvoice($invoiceId);
        $this->exportJson($result, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }
}
